import java.sql.*;
import java.util.*;

public class TopMenu
{
	private Connection connection;
	private Statement statement;
	private ResultSet resultSet;
	private String query;
	
	public TopMenu(String username, String password)
	{
		connection = ConnectionManager.getConnection(username, password);
		statement = ConnectionManager.createStatement(connection);
		ConsoleMenu();	
		ConnectionManager.closeConnection(connection);
	}
	
	public void ConsoleMenu()
	{
		String login;
		String password;
		String temp;
		
		try
		{			
			while (true)
			{
				login = Keyin.inString(" Enter login: ");
				password = Keyin.inString(" Enter password: ");
				
				query = "SELECT Password FROM ADMINISTRATOR WHERE Login='"+login+"'";
				resultSet = statement.executeQuery(query);
				
				if (resultSet.next())
				{
					temp = resultSet.getString(1);
					if (password.equals(temp))
					{
						Administrator admin = new Administrator(connection, statement);
						break;
					}
					else
					{
						System.out.println(" Password error.");
						continue;
					}
				}
				else
				{
					query = "SELECT Password FROM CUSTOMER WHERE Login='"+login+"'";
					resultSet = statement.executeQuery(query);
					
					if (resultSet.next())
					{
						temp = resultSet.getString(1);
						if (password.equals(temp))
						{
							Customer c = new Customer(connection, statement, login);							
							break;
						}
						else
						{
							System.out.println(" Password error.");
							continue;
						}						
					}
					else
					{
						System.out.println(" Login error.");
						continue;
					}
				}
			}

			connection.commit();
		}
		catch(Exception Ex)
		{
		  System.out.println("Error running the sample queries.  Machine Error: "+Ex.toString());
		}											
	}
	
	public static void main(String[] args)
	{
		String username, password;
		
		username = Keyin.inString(" Enter database username: ");
		password = Keyin.inString(" Enter database password: ");
	
		TopMenu t = new TopMenu(username, password);
	}	
}
